package com.kiven.beanutils;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.io.File;
import java.util.*;

public class Debug {
	private static Logger defaultLogger = Logger.getLogger(Debug.class);
	private static HashMap<String,Logger> loggers;
	
	static {
		File localFile = new File("c:/om_log/log4j.properties");
		if (localFile.exists()) {
			PropertyConfigurator.configure(localFile.getPath());
		} else {
			// 默认的路径下面没有log4j属性文件,则会去classLoader下面找
			PropertyConfigurator.configure("log4j.properties");
		}
		loggers = new HashMap();
	}
	
	
	public static Logger getLogger() {
		return defaultLogger;
	}
	
	public static Logger getLogger(String paramString) {
		return getLogger(paramString,true);
	}
	
	public static Logger getLogger(String paramString,boolean paramBoolean) {
		if (!loggers.containsKey(paramString)) {
			Logger localLogger = Logger.getLogger(paramString);
			if (!(localLogger.getAllAppenders().hasMoreElements()) && (paramBoolean == true))
				return defaultLogger;
			localLogger.setAdditivity(false);
			loggers.put(paramString, localLogger);
			return localLogger;
		}
		if (paramBoolean == true) {
			return defaultLogger;
		}
		return loggers.get(paramString);
	}
	
	public static boolean containsLogger(String paramString) {
		Logger localLogger = getLogger(paramString,false);
		return localLogger != defaultLogger;
	}
	
	public static void printStackTrace(Logger paramLogger)
	  {
	    StackTraceElement[] arrayOfStackTraceElement = (StackTraceElement[])Thread.getAllStackTraces().get(Thread.currentThread());
	    for (int i = 3; i < arrayOfStackTraceElement.length; i++)
	    {
	      StackTraceElement localStackTraceElement = arrayOfStackTraceElement[i];
	      paramLogger.error(localStackTraceElement.getClassName() + "=" + localStackTraceElement.getMethodName() + "=" + localStackTraceElement.getLineNumber());
	    }
	  }
	
//	public static void main(String[] args) {
//		StackTraceElement[] arrayOfStackTraceElement = (StackTraceElement[])Thread.getAllStackTraces().get(Thread.currentThread());
//	    for (int i = 0; i < arrayOfStackTraceElement.length; i++)
//	    {
//	      StackTraceElement localStackTraceElement = arrayOfStackTraceElement[i];
//	      System.out.println(localStackTraceElement.getClassName() + "=" + localStackTraceElement.getMethodName() + "=" + localStackTraceElement.getLineNumber());
//	    }
//	}
	
}
